<template>
  <div class="box">
    <span class="name" style="">项目名称:</span>
    <span class="text">{{ endTime.project_name }}</span>
    <span class="name" style="">开标倒计时:</span>
    <span class="text" style="color:red"> {{ time }}</span>
  </div>
</template>

<script>
export default {
  props: {
    endTime: {
      type: Object,
      default: function() {
      }
    }
  },
  data() {
    return {
      time: ''
    }
  },
  mounted() {
    const time = setInterval(() => {
      if (this.flag == true) {
        clearInterval(time)
      }
      this.timeDown(this.endTime.bid_open_time)
    }, 500)
  },
  methods: {
    timeDown(endTimes) {
      const reg = new RegExp('-', 'g')
      const newTimes = endTimes.replace(reg, '/')
      // console.log(newTimes);
      const endTime = new Date(newTimes)
      const nowTime = new Date()
      const leftTime = parseInt((endTime.getTime() - nowTime.getTime()) / 1000)
      const d = parseInt(leftTime / (24 * 60 * 60))
      const h = this.formate(parseInt((leftTime / (60 * 60)) % 24))
      const m = this.formate(parseInt((leftTime / 60) % 60))
      const s = this.formate(parseInt(leftTime % 60))
      if (leftTime <= 0) {
        this.flag = true
        this.$emit('time-end')
      }
      this.time = `${d}天${h}小时${m}分${s}秒`
      // this.time = `${d}天${h}小时`;
    },
    formate(time) {
      if (time >= 10) {
        return time
      } else {
        return `0${time}`
      }
    }
  }
}
</script>

<style scoped>
.box{
  padding: 8px 10px;
}
.box span{
  margin-right: 15px;
}
.name {
  color: rgba(0, 0, 0, 0.45);
  font-size: 16px;
  cursor: pointer;
}
.time {
  color: rgba(0, 0, 0, 0.45);
  font-size: 16px;
  cursor: pointer;
}
</style>
